const commentService = require('@/services/comment.service')
const JsonResult = require('@/utils/httpResponse.util')

class CommentController {
  /**
   * 文章评论分页列表查询
   * @param {*} req
   * @param {*} response
   */
  async findArticleComments (req, response) {
    const { pageNo, pageSize, searchKeywords } = req.query
    try {
      const data = await commentService.findPage(pageNo, pageSize, searchKeywords, 'article')
      JsonResult.success({
        req,
        response,
        data,
        message: '查询成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '查询失败' })
    }
  }

  /**
   * 友圈评论分页列表查询
   * @param {*} req
   * @param {*} response
   */
  async findCircleComments (req, response) {
    const { pageNo, pageSize, searchKeywords } = req.query
    try {
      const data = await commentService.findPage(pageNo, pageSize, searchKeywords, 'circle')
      JsonResult.success({
        req,
        response,
        data,
        message: '查询成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '查询失败' })
    }
  }

  /**
   * 文章评论删除
   * @param {*} req
   * @param {*} response
   * @returns
   */
  async deleteArticleComment (req, response) {
    const id = req.query.id
    try {
      const data = await commentService.delete('article', id)
      if (data?.errorMessage) {
        return JsonResult.fail({ req, response, message: data.errorMessage })
      }
      JsonResult.success({
        req,
        response,
        data,
        message: '删除成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '删除失败' })
    }
  }

  /**
   * 友圈评论删除
   * @param {*} req
   * @param {*} response
   * @returns
   */
  async deleteCircleComment (req, response) {
    const id = req.query.id
    try {
      const data = await commentService.delete('circle', id)
      if (data?.errorMessage) {
        return JsonResult.fail({ req, response, message: data.errorMessage })
      }
      JsonResult.success({
        req,
        response,
        data,
        message: '删除成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '删除失败' })
    }
  }
}

module.exports = new CommentController()
